package com.softwarerusher.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.softwarerusher.daoImpl.BookDAO;
import com.softwarerusher.daoImpl_cym.TCategoryDAO;
import com.softwarerusher.model.TBook;
import com.softwarerusher.model.TCategory;

public class netshopcategory extends ActionSupport implements RequestAware,SessionAware{
    private  int category;
    private  int requestpagenum;
    private Map<String,Object> request;
    private Map<String,Object> session;
    private List<TBook> bookList;
    private int totalnum = 0 ;

    static private int pagesize = 2;
    private int currentpage;
    
    
 
	public String category()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	TCategory category1 = cate.findById(category);
    	
        //System.out.print("dy"+result.size());
        
    	bookList = BookDAO.paging(result, 1, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", "1");
    	session.put("pagesize", pagesize);
    	return "success";
    }
    public String nextpage()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	//TCategory category1 = cate.findById(category);
        System.out.print("yi"+requestpagenum);
    	bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	//session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
    	session.put("pagesize", pagesize);
       	return "success";
    }
    public String previouspage()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	//TCategory category1 = cate.findById(category);
        System.out.print("yi"+requestpagenum);
    	bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	//session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
    	session.put("pagesize", pagesize);
       	return "success";
    }
    public String firstpage()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	//TCategory category1 = cate.findById(category);
        System.out.print("yi"+requestpagenum);
    	bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	//session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
    	session.put("pagesize", pagesize);
       	return "success";

    }
    public String lastpage()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	//TCategory category1 = cate.findById(category);
        System.out.print("yi"+requestpagenum);
    	bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	//session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
    	session.put("pagesize", pagesize);

       	return "success";
    }
    public String pagenum()
    {
    	System.out.print(category);
    	
    	TCategoryDAO cate = new TCategoryDAO();
    	
    	  	
    	List<TBook> result = BookDAO.queryBookByCategoryId(-1, String.valueOf(category));
    	//TCategory t  = cate.findById(category);
    	//System.out.print(t.getCategoryName());
    	
    	//TCategory category1 = cate.findById(category);
        System.out.print("yi"+requestpagenum);
    	bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	//session.put("category", category1.getCategoryName());
    	session.put("categoryID", category);
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
    	session.put("pagesize", pagesize);
       	return "success";

    }

	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}


	public int getCategory() {
		return category;
	}


	public void setCategory(int category) {
		this.category = category;
	}
	public int getRequestpagenum() {
		return requestpagenum;
	}
	public void setRequestpagenum(int requestpagenum) {
		this.requestpagenum = requestpagenum;
	}

	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
	public List<TBook> getBookList() {
		return bookList;
	}
	public void setBookList(List<TBook> bookList) {
		this.bookList = bookList;
	}
	public int getTotalnum() {
		return totalnum;
	}
	public void setTotalnum(int totalnum) {
		this.totalnum = totalnum;
	}
    
    
	   public int getCurrentpage() {
			return currentpage;
		}
		public void setCurrentpage(int currentpage) {
			this.currentpage = currentpage;
		}
	
	
}
